package com.hust.bridgecheck.serviceImpl.UserManagement;

import com.hust.bridgecheck.entity.CustomUserData;
import com.hust.bridgecheck.entity.User;
import com.hust.bridgecheck.entity.UserGroup;
import com.hust.bridgecheck.repository.GroupRepository;
import com.hust.bridgecheck.repository.UserRepository;
import com.hust.bridgecheck.service.UserManagement.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private GroupRepository groupRepository;

    @Override
    public User addUser(User user){
        user.setCreateTime(new Timestamp(new Date().getTime()));
        return userRepository.save(user);
    }

    @Override
    public void deleteUser(Integer userID){

        userRepository.deleteById(userID);
    }

    @Override
    public List<User> getAllUser(){

        return userRepository.findAll();
    }

    @Override
    public List<CustomUserData> getUserAnGroupInfo() {

        List<CustomUserData> list = new ArrayList<>();
        List<User> users = userRepository.findAll();
        CustomUserData customUserData;
        for (User u :users){
            customUserData = new CustomUserData();
            Optional<UserGroup> group = groupRepository.findById(u.getGroupID());
           if (!group.isPresent())
                continue;
            UserGroup userGroup = group.get();
            customUserData.setUser(u);
            customUserData.setUserGroup(userGroup);
            list.add(customUserData);
        }

        return list;
    }

    @Override
    public void generateUser(Integer userNum, Integer groupId) {

        for (int i = 0;i<userNum;i++){
            User user = new User();
            user.setPassWd("123456");
            user.setTel("default");
            user.setMail("default");
            user.setCreateTime(new Timestamp(new Date().getTime()));
            user.setUserAccount(String.valueOf(System.currentTimeMillis()));
            user.setGroupID(groupId);
            user.setUserName("用户"+user.getUserAccount());
            userRepository.save(user);
        }
    }

    @Override
    public User getUserByUserAccount(String UserAccount) {
        return userRepository.findUserByUserAccount(UserAccount);
    }

    @Override
    public void updateUser(User user) {

        User u = userRepository.findById(user.getUserID()).get();
        u.setGroupID(user.getGroupID());
        u.setMail(user.getMail());
        u.setPassWd(user.getPassWd());
        u.setTel(user.getTel());
        u.setUserAccount(user.getUserAccount());
        u.setUserName(user.getUserName());

        userRepository.save(u);

    }

    @Override
    public User getUserByID(Integer userID) {

        return userRepository.findById(userID).get();
    }


}

